﻿using System;
using System.Collections.Generic;
using System.Text;
using CommunityServer.WebServices.MediaGalleries;
using System.Windows.Forms;

namespace nb.development.csmediamanager
{
    public class Deleter
    {
        public Deleter(List<int> postids, Guid siteid)
        {
            this.postids = postids;
            this.siteid = siteid;
        }

        private Guid siteid;
        private List<int> postids;
        public event DeletedPost Deleted;

        public void Run()
        {
            try
            {
                MediaGalleriesService mgs = Sites.Instance[this.siteid].Service;
                int x = 1;
                foreach (int postid in postids)
                {
                    try
                    {
                        mgs.DeleteMediaGalleryPost(postid);
                        if (Deleted != null)
                            Deleted(x, postids.Count, null);
                    }
                    catch (Exception ex)
                    {
                        if (Deleted != null)
                            Deleted(x, postids.Count, ex);
                        else throw ex;
                    }
                    x++;
                }
            }
            catch (Exception e) { MessageBox.Show("Error:\n" + e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        }
    }

    public delegate void DeletedPost(int index, int count, Exception e);
}
